﻿using KinonekoSoftware.CodariaDB.Documents.Keywords;
using KinonekoSoftware.Studio.Pages.Instances.Geography;

// ReSharper disable RedundantArgumentDefaultValue

namespace KinonekoSoftware.Studio.Pages.Galleries
{
    [NeedLocalised]
    public partial class ModernListGalleryViewModel : GalleryScraffolds
    {


        private           InstanceCache _currentSelected;
        private           int           _pageIndex;
        private           int           _pageCount;


        public ModernListGalleryViewModel()
        {
            
            SelectAvatarCommand = CreateCommand<InstanceCache>(DoSelectAvatarCommand);
            PreviousPageCommand = CreateCommand(DoPreviousPageCommand);
            NextPageCommand     = CreateCommand(DoNextPageCommand);
        }




        //-------------------------------------------------------------
        //
        //          Override Methods
        //
        //-------------------------------------------------------------


        protected virtual void OnInstanceCacheSelected(InstanceCache oldValue, InstanceCache newValue)
        {

        }



        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------

        private async void DoSelectAvatarCommand(InstanceCache target)
        {

            var r = await Platform.Avatar();


            if (!r.IsFinished)
            {
                if (!string.IsNullOrEmpty(r.Reason)) await Danger(TitleSR.DangerOperation, r.Reason);
                return;
            }

            var old    = target.Avatar;
            var ief    = r.Value;
            var id     = ief.ImageID;
            var buffer = ief.ImageBuffer;

            var gravatar = Platform.GetEngine<GravatarSystem>();

            //
            //
            await gravatar.WriteAsync(id, buffer);


            gravatar.InsertOccupation(id);
            gravatar.RemoveOccupation(old);


            //
            //
            target.Avatar = id;

            //
            //
            Core.EngineOfInstance
                .UpdateCache(target);
        }

        
        private void DoPreviousPageCommand()
        {
            if (!Core.CanPreviousPage())
            {
                return;
            }

            PageIndex -= 1;
            PushToView(string.Empty);
            Core.Paging(PageIndex);
        }

        private void DoNextPageCommand()
        {
            if (!Core.CanNextPage())
            {
                return;
            }

            PageIndex += 1;
            PushToView(string.Empty);
            Core.Paging(PageIndex);
        }

        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------
        




        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------

        public int PageIndex
        {
            get => _pageIndex;
            set
            {
                SetValue(ref _pageIndex, Math.Clamp(value, 1, PageCount));
                Core.PageIndex = value;
            }
        }


        public int PageCount
        {
            get => _pageCount;
            private set
            {
                SetValue(ref _pageCount, value);
                Core.PageCount = value;
            }
        }




        public InstanceCache CurrentSelected
        {
            get => _currentSelected;
            set
            {
                var old = _currentSelected;

                //
                //
                SetValue(ref _currentSelected, value);
                PushToView();

                //
                //
                OnInstanceCacheSelected(old, _currentSelected);
            }
        }


        protected virtual void IsListModeChanged(bool isFully)
        {

        }


        //-------------------------------------------------------------
        //
        //          Commands
        //
        //-------------------------------------------------------------



        public ICommandEX SelectAvatarCommand { get; }
        public ICommandEX NextPageCommand     { get; }
        public ICommandEX PreviousPageCommand { get; }



        public string InstanceID => CurrentSelected?.Id;

    }
}